<template>
  <el-dialog
    append-to-body
    :title="!dataForm.id ? '入住' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <div class="center">{{dataForm.parentName}}--{{dataForm.roomNum}}</div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学生id" prop="studentId">

      <el-select class="select" v-model="dataForm.studentId" filterable placeholder="请选择">
        <el-option
          v-for="item in studentList"
          :key="item.sid"
          :label="item.name +' 学号: ' + item.sid"
          v-html="item.name +'&nbsp;&nbsp;&nbsp;&nbsp; 学号: ' + item.sid"
          :value="item.sid">
        </el-option>
      </el-select>

    </el-form-item>
    <el-form-item label="宿舍号"  prop="roomId">
      <el-input :readonly="true" v-model="dataForm.roomId" placeholder="宿舍号"></el-input>
    </el-form-item>
    <el-form-item :readonly="true" label="床铺号" prop="bedId">
      <el-input v-model="dataForm.bedId" placeholder="床铺号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { dormitoryArrangementEntityApi, studentApi } from '@/api/student'

  export default {
    data () {
      return {
        visible: false,
        studentList:[],
        dataForm: {
          id: 0,
          studentId: '',
          roomId: '',
          bedId: '',
          parentName:'',
          roomNum:''
        },
        dataRule: {
          studentId: [
            { required: true, message: '学生id不能为空', trigger: 'blur' }
          ],
          roomId: [
            { required: true, message: '宿舍号不能为空', trigger: 'blur' }
          ],
          bedId: [
            { required: true, message: '床铺号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (data,id,sid) {
        console.log(data)

        this.visible = true
        this.$nextTick(() => {
          this.dataForm= this.$options.data().dataForm
          if (id) {
            this.dataForm.studentId = sid;
            this.dataForm.id = id || 0
          }

          this.dataForm.bedId = data.bedId
          this.dataForm.roomId = data.roomId
          this.dataForm.parentName = data.parentName
          this.dataForm.roomNum = data.roomNum
        })
        studentApi.listName()
        .then(({data})=>{
          this.studentList = data
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            dormitoryArrangementEntityApi.saveOrUpdate( this.dataForm,!this.dataForm.id ? 'save' : 'update')
            .then(data => {
              if (data && data.code == 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss">
.center{
  text-align: center;
  font-size: 35px;
  line-height: 40px;
  margin-bottom: 10px;
}
</style>
